﻿namespace NTLib.Core.UTest.Configuration.Attributes
{
    using Core.Configuration;
    using Core.Configuration.Attributes;
    using DataSample;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.Threading.Tasks;
    using NTLib.Core.Reflection;
    using Core.Configuration.Values;

    [TestClass]
    public class TypeImportUTest
    {
        #region Fields

        private const string TESTKEY = "poney";

        #endregion

        #region Neasted

        public interface ITest { }

        public interface ITest2 { }

        [TypeExportConfiguration(typeof(ITest))]
        public class ImplTest : ITest { }

        [TypeExportConfiguration(typeof(ITest), Key = TESTKEY)]
        public class ImplTest1 : ITest { }

        [TypeExportConfiguration(typeof(ITest2))]
        [TypeExportConfiguration(typeof(ITest), Key = TESTKEY)]
        public class ImplTest1and2 : ITest, ITest2 { }

        [TypeExportConfiguration(typeof(ITest2))]
        public class ImplNothing { }

        [TypeImportConfiguration(typeof(ITest))]
        class ToBeConfigureImportTest : ToBeConfigured
        {
        }

        [TypeImportConfiguration(typeof(ITest2))]
        class ToBeConfigureImportTest2 : ToBeConfigured
        {
        }

        #endregion

        #region Methods

        [TestCategory(ConfigManagerUTest.CONFIG_CATEGORIE)]
        [TestMethod]
        public void Config_TypeImport()
        {
            ConfigurationCollection collection = null;

            var testAttr = typeof(ToBeConfigureImportTest).GetMandatoryAttribute<TypeImportConfigurationAttribute>();

            Assert.IsNotNull(testAttr);

            collection = testAttr.CollectConfigurationValue(typeof(ToBeConfigureImportTest));

            Assert.IsNotNull(collection);
            Assert.AreEqual(collection.Count, 2);
            Assert.AreEqual(collection[""].Count, 1);
            Assert.AreEqual(collection[TESTKEY].Count, 2);

            Assert.AreEqual(collection[""].FirstOrDefault().GetType(), typeof(TypeConfig));
            Assert.AreEqual(collection[TESTKEY].FirstOrDefault().GetType(), typeof(TypeConfig));

            Assert.AreEqual(((TypeConfig)collection[""].FirstOrDefault()).Value, typeof(ImplTest));
            Assert.IsTrue(collection[TESTKEY].Any(el => ((TypeConfig)el).Value == typeof(ImplTest1and2)));
            Assert.IsTrue(collection[TESTKEY].Any(el => ((TypeConfig)el).Value == typeof(ImplTest1)));

            var testAttr2 = typeof(ToBeConfigureImportTest2).GetMandatoryAttribute<TypeImportConfigurationAttribute>();

            collection = testAttr2.CollectConfigurationValue(typeof(ToBeConfigureImportTest2));

            Assert.IsNotNull(collection);
            Assert.AreEqual(collection.Count, 1);
            Assert.AreEqual(collection[""].Count, 2);

            Assert.IsTrue(collection[""].Any(el => ((TypeConfig)el).Value == typeof(ImplTest1and2)));
            Assert.IsTrue(collection[""].Any(el => ((TypeConfig)el).Value == typeof(ImplNothing)));
        }

        #endregion
    }
}
